package com.iwords.android;

import java.util.ArrayList;
import com.iwords.android.R;
import android.content.res.Resources;
import android.graphics.drawable.Drawable;
import android.widget.TextView;

public class LevelViews {
	/**7个LevelView*/
	private ArrayList<LevelView> levelViews = new ArrayList<LevelView>();
	private ShowWord showWord;
	public LevelViews(ShowWord showWord){
		this.showWord = showWord;
		TextView knownNum = (TextView)showWord.findViewById(R.id.word_known_num);
		TextView level5Num = (TextView)showWord.findViewById(R.id.word_level5_num);
		TextView level4Num = (TextView)showWord.findViewById(R.id.word_level4_num);
		TextView level3Num = (TextView)showWord.findViewById(R.id.word_level3_num);
		TextView level2Num = (TextView)showWord.findViewById(R.id.word_level2_num);
		TextView level1Num = (TextView)showWord.findViewById(R.id.word_level1_num);
		TextView leftNum = (TextView)showWord.findViewById(R.id.word_left_num);
		TextView knownReviewNum = (TextView)showWord.findViewById(R.id.word_known_review);
		TextView level5ReviewNum = (TextView)showWord.findViewById(R.id.word_level5_review);
		TextView level4ReviewNum = (TextView)showWord.findViewById(R.id.word_level4_review);
		TextView level3ReviewNum = (TextView)showWord.findViewById(R.id.word_level3_review);
		TextView level2ReviewNum = (TextView)showWord.findViewById(R.id.word_level2_review);
		TextView level1ReviewNum = (TextView)showWord.findViewById(R.id.word_level1_review);
		TextView leftReviewNum = (TextView)showWord.findViewById(R.id.word_left_review);
		TextView known = (TextView)showWord.findViewById(R.id.word_known);
		TextView level5 = (TextView)showWord.findViewById(R.id.word_level5);
		TextView level4 = (TextView)showWord.findViewById(R.id.word_level4);
		TextView level3 = (TextView)showWord.findViewById(R.id.word_level3);
		TextView level2 = (TextView)showWord.findViewById(R.id.word_level2);
		TextView level1 = (TextView)showWord.findViewById(R.id.word_level1);
		TextView left = (TextView)showWord.findViewById(R.id.word_left);
		
		LevelView knownView = new LevelView(known, knownNum, knownReviewNum, 0);
		LevelView level5View = new LevelView(level5, level5Num, level5ReviewNum, 1);
		LevelView level4View = new LevelView(level4, level4Num, level4ReviewNum, 2);
		LevelView level3View = new LevelView(level3, level3Num, level3ReviewNum, 3);
		LevelView level2View = new LevelView(level2, level2Num, level2ReviewNum, 4);
		LevelView level1View = new LevelView(level1, level1Num, level1ReviewNum, 5);
		LevelView leftView = new LevelView(left, leftNum, leftReviewNum, 6);
		
		this.levelViews.add(knownView);
		this.levelViews.add(level5View);
		this.levelViews.add(level4View);
		this.levelViews.add(level3View);
		this.levelViews.add(level2View);
		this.levelViews.add(level1View);
		this.levelViews.add(leftView);
		
		addKnownLevelListener();
		
	}
	/**注册KnownLevel切换点击监听。*/
	private void addKnownLevelListener(){
		for(LevelView level : this.levelViews){
			LevelViewsListener listener = new LevelViewsListener(level, showWord);
			level.levelName.setOnClickListener(listener);
			level.levelCount.setOnClickListener(listener);
			level.levelReviewCount.setOnClickListener(listener);
			//监听levelCount变化，插入动画效果
			//level.levelCount.addTextChangedListener(new LevelCountChangeListener(ShowWord.this, level.levelCount));
		}
	}
	/**刷新词库状态数据*/
	void refreshLevelCount(){
		IWordsApp app = ((IWordsApp) showWord.getApplicationContext());
		//System.out.println("LevelViews.refreshLevelCount() app.getCurrentLibStatus() = "+app.getCurrentLibStatus());
		for(int i=0; i<7; i++){
			//System.out.println("LevelViews.refreshLevelCount() i = "+i+" app.getCurrentLibStatus().getGreenCount(i) = "+app.getCurrentLibStatus().getGreenCount(i));
			levelViews.get(i).levelCount.setText(app.getCurrentLibStatus().getRedCount(i)+"");
			levelViews.get(i).levelReviewCount.setText(app.getCurrentLibStatus().getGreenCount(i)+"");
		}
	}
	
	public ArrayList<LevelView> getLevelViews() {
		return levelViews;
	}
	public void setLevelViews(ArrayList<LevelView> levelViews) {
		this.levelViews = levelViews;
	}

	public void highlight(int levelIndex){
		Resources resource = showWord.getBaseContext().getResources();
        Drawable backgroundColor = resource.getDrawable(R.color.showword_background_color);
        //Drawable levelNameColor = resource.getDrawable(R.color.showword_levelname_color);
        //Drawable reviewColor = resource.getDrawable(R.color.showword_review_color);
        //Drawable numColor = resource.getDrawable(R.color.showword_num_color);
        resource.getColorStateList(R.color.showword_levelname_color);
		for(LevelView level : levelViews){
			level.levelName.setTextColor(resource.getColor(R.color.showword_levelname_color));
			//level.levelCount.setTextColor(Color.RED);
			//level.levelReviewCount.setTextColor(Color.RED);
			level.levelName.setBackgroundDrawable(backgroundColor);
			level.levelCount.setBackgroundDrawable(backgroundColor);
			level.levelReviewCount.setBackgroundDrawable(backgroundColor);
		}
		LevelView level = this.levelViews.get(levelIndex);
		int hilightTextColor = resource.getColor(R.color.hilight_text_color);
		level.levelName.setTextColor(hilightTextColor);
		//level.levelCount.setTextColor(hilightTextColor);
		//level.levelReviewCount.setTextColor(hilightTextColor);
		level.levelName.setBackgroundColor(resource.getColor(R.color.hilight_background_color));
		level.levelCount.setBackgroundColor(resource.getColor(R.color.hilight_background_color));		
		level.levelReviewCount.setBackgroundColor(resource.getColor(R.color.hilight_background_color));		
	}
}
